/*
 * Copyright 2025 arisgo@163.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.arisgo.cloud.web.param;


import com.arisgo.cloud.web.model.NumberId;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import java.util.List;
import java.util.Map;

/**
 * 分页参数
 *
 * @author Coulson
 * @since 2023-6-15
 */
public class Paging extends Search {

    private int pageNo = 1;

    private int pageSize = 10;

    public Paging() {
    }

    public Paging(List<Param> params) {
        this(null, params, null);
    }

    public Paging(Selected selected, List<Param> params, List<Sort> sorts) {
        this(selected, params, sorts, 1, 10);
    }

    public Paging(Selected selected, List<Param> params, List<Sort> sorts, int pageNo, int pageSize) {
        super(selected, params, sorts);
        this.pageNo = pageNo;
        this.pageSize = pageSize;
    }

    public static Paging creator(Param... params) {
        return creator(Lists.newArrayList(params));
    }

    public static Paging creator(List<Param> params) {
        return creator(null, params);
    }

    public static Paging creator(Sort sort, Param... params) {
        return creator(null, Lists.newArrayList(params), Lists.newArrayList(sort));
    }

    public static Paging creator(Selected selected, List<Param> params) {
        return creator(selected, params, Lists.newArrayList(Sort.desc(NumberId.ID)));
    }

    public static Paging creator(Selected selected, List<Param> params, List<Sort> sorts) {
        return creator(selected, params, sorts, 1, 10);
    }

    public static Paging creator(Selected selected, List<Param> params, List<Sort> sorts, int pageNo, int pageSize) {
        return new Paging(selected, params, sorts, pageNo, pageSize);
    }

    @Override
    public Map<String, Object> convertMap() {
        Map<String, Object> map = super.convertMap();
        // 当前页
        map.put("pageNo", pageNo - 1);
        // 每页展示数据量
        map.put("pageSize", pageSize);
        // 从第几条数据开始（包含）
        map.put("offset", getOffset());
        // 到第几条数据结束（不包含）
        map.put("end", getEnd());
        return map;
    }

    public int getPageNo() {
        return pageNo;
    }

    public void setPageNo(int pageNo) {
        this.pageNo = pageNo;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    /**
     * 从第几条数据开始查询
     */
    public int getOffset() {
        return (pageNo - 1) * pageSize;
    }

    /**
     * 到第几条数据结束
     */
    public int getEnd() {
        return pageNo * pageSize;
    }

}
